﻿using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharePointUtilitiesForDevelopers.Action;
using SharePointUtilitiesForDevelopers.Actions.Forms;
using SharePointUtilitiesForDevelopers.Service;

namespace SharePointUtilitiesForDevelopers.Actions
{
    /// <summary>
    /// Generates a url for calling owssvr.dll.
    /// </summary>
    public class GenerateOwsSvrUrlAction: IAction
    {
        /// <summary>
        /// Gets the title.
        /// </summary>
        /// <value>The title.</value>
        public string Title
        {
            get { return Properties.Resources.GenerateOwsSvrUrlActionTitle; }
        }

        /// <summary>
        /// Gets the description.
        /// </summary>
        /// <value>The description.</value>
        public string Description
        {
            get { return Properties.Resources.GenerateOwsSvrUrlActionDescription; }
        }

        /// <summary>
        /// Gets the target.
        /// </summary>
        /// <value>The target.</value>
        public ActionTarget Target
        {
            get { return ActionTarget.List | ActionTarget.ListView; }
        }

        /// <summary>
        /// Executes the action.
        /// </summary>
        /// <param name="actionContext">The context.</param>
        public void Execute(ActionContext actionContext)
        {
            string url = null;

            if(actionContext.Entity is SharePointList)
            {
                var list = (SharePointList)actionContext.Entity;
                url = GenerateOwsSvrUrl(actionContext.Web.Url, list);
            }

            if (actionContext.Entity is SharePointListView)
            {
                var view = (SharePointListView)actionContext.Entity;
                url = GenerateOwsSvrUrl(actionContext.Web.Url, view);
            }

            if(url != null)
            {
                var form = ServiceLocator.Resolve<UrlForm>();
                form.Url = url;
                form.UrlDescription = Description;
                form.ShowDialog(actionContext.Owner as IWin32Window);
            }
        }

        /// <summary>
        /// Generates the owssvr URL.
        /// </summary>
        /// <param name="siteUrl">The site URL.</param>
        /// <param name="list">The list.</param>
        /// <returns>The owssvr URL.</returns>
        private static string GenerateOwsSvrUrl(string siteUrl, SharePointList list)
        {
            return GenerateOwsSvrUrl(siteUrl, list.Name, null);
        }

        /// <summary>
        /// Generates the owssvr URL.
        /// </summary>
        /// <param name="siteUrl">The site URL.</param>
        /// <param name="view">The list view.</param>
        /// <returns>The owssvr URL.</returns>
        private static string GenerateOwsSvrUrl(string siteUrl, SharePointListView view)
        {
            return GenerateOwsSvrUrl(siteUrl, view.ListName, view.Name);
        }

        /// <summary>
        /// Generates the owssvr URL.
        /// </summary>
        /// <param name="siteUrl">The site URL.</param>
        /// <param name="listName">The list id.</param>
        /// <param name="viewName">The view id.</param>
        /// <returns>The owssvr URL.</returns>
        private static string GenerateOwsSvrUrl(string siteUrl, string listName, string viewName)
        {
            var owsSvrUrl = new StringBuilder();
            owsSvrUrl.Append(SharePointServiceUrl.GetServiceUrl(siteUrl, SharePointServiceUrl.OwsSvrUrl));
            owsSvrUrl.Append("?");
            owsSvrUrl.Append("Cmd=Display");
            owsSvrUrl.Append("&XMLDATA=true");

            if(!string.IsNullOrEmpty(listName))
            {
                // Add the list parameter (the dashes in the guid must be url encoded, and dashes are not encoded in the HttpUtility implementation)
                owsSvrUrl.AppendFormat("&List={0}", SharePointServiceUrl.UrlEncode(listName.ToUpperInvariant(), (new[] {'-'}).AsQueryable()));
            }
            
            if(!string.IsNullOrEmpty(viewName))
            {
                // Add the view parameter (the dashes in the guid must be url encoded, and dashes are not encoded in the HttpUtility implementation)
                owsSvrUrl.AppendFormat("&View={0}", SharePointServiceUrl.UrlEncode(viewName.ToUpperInvariant(), (new [] {'-'}).AsQueryable()));
            }

            return owsSvrUrl.ToString();
        }
    }
}